public class TestMemberStatus {

	/*=========================================
	test methods for trigger MemberStatusRollup
	
	other dependencies:
		class UnitTestData
			uses: trigger CreateContactRoles
			(for creating Opps w/ contact roles auto-created)
				uses: class OppContactRoles
		class ContactsInHouseholds
			and many related triggers/classes
			(for the household-based test)
		class Constants
		class DefaultAccount
	==========================================*/
	
	// TBD - undelete (written, just needs to be uncommented when winter 08 is live)
	// TBD - test changing the member dates on existing opps


	static testMethod void testNewMemberships () {
	
		// for a single contact w/ no previous mbrships, add a new membership
		// and test mbr stats are created
	
		integer howMany = 1;
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
//		system.debug (TestCons);
		
		// create a new membership opp(s)
		Opportunity[] newOpps = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datClose, 100 , 'Membership');

		//now set membership fields
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// note for member level it's not required that the value actually be in the picklist
			// this won't work in the UI but works fine for tests
			newOpps[i].Member_Level__c = 'Phony Status';
			newOpps[i].Membership_Origin__c = 'New';
		}

		// insert the opp(s)
//		Test.StartTest();
		insert newOpps;
		
		// now, while we're at it, put in another, non-membership opp with the mbrship fields populated
		// this may never happen in production (prevented by validation?) but could happen w/ import, etc
		// tests that the rollups are really looking only at membership-type opps
		Opportunity[] nonMbrOpp = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datClose, 555 , 'Gift');
		//now set membership fields
		for ( integer i=0;i<nonMbrOpp.size();i++) {
			nonMbrOpp[i].Membership_Start_Date__c = datClose;
			nonMbrOpp[i].Membership_End_Date__c = datClose.AddYears(2);
			// note for member level it's not required that the value actually be in the picklist
			// this won't work in the UI but works fine for tests
			nonMbrOpp[i].Member_Level__c = 'Phony Status';
			nonMbrOpp[i].Membership_Origin__c = 'Renewal';
		}		
		
		// reset contact roles flag and insert non-mbr opps
		OppContactRoles.haveCheckedContactRoles = false;
		insert nonMbrOpp;
		
		//now test that the first contact has received the proper member stats from the trigger
		id FirstConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];
		
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( 1 , UpdatedCon.Membership_Span__c );
		System.AssertEquals ( 'New' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );
		System.AssertEquals ( 'Phony Status' , UpdatedCon.Member_Level__c );
		
		//also make sure that no member status has been given to the Individual account
		// new method to lookup individual acct id
		Id IndAcctId = DefaultAccount.GetIndividualAccountId();
				
		Account IndAcct = [SELECT id, Name, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Account WHERE Id = :IndAcctId LIMIT 1];
		// tbd also include Membership_Status__c in select once added
		
		System.AssertNotEquals ( null, IndAcct.Name );
		System.AssertEquals ( null , IndAcct.Membership_End_Date__c );
		System.AssertEquals ( null , IndAcct.Membership_Join_Date__c );
		System.AssertEquals ( null , IndAcct.Member_Level__c );
		
		
		//now delete the Opp - member status should disappear
		delete newOpps;
//		Test.StopTest();
		
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];
		
//		system.debug ('updated Con after delete: ' + UpdatedCon);
		
		System.AssertEquals ( null , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( null , UpdatedCon.Membership_Origin__c );
		
		/* commented out until Winter 08 is active, since undelete not avail until then
		
		//finally, undelete the opps - member status should reappear
		undelete newOpps;
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( datClose , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );
		*/
		
		// and finally chg the non-mbrship opp to a mbrship opp
		// this should include in the calcs and push back the end date
		
		// first, lookup the rectype id for membership
		RecordTypes rtlookup = new RecordTypes ('Opportunity');
		id rtid = rtlookup.GetRecordTypeId ('Membership');
		
		for ( Opportunity thisOpp : nonMbrOpp ) {
			thisOpp.RecordTypeID = rtid;	
		}
		update nonMbrOpp;
		
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];
		System.AssertEquals ( datClose.AddYears(2) , UpdatedCon.Membership_End_Date__c );
				
	} 


	static testMethod void testNewMembershipHousehold () {
	
		// for a single contact w/ no previous mbrships but multiple HH relations, 
		// add a new membership
		// and test mbr stats are created for both contacts
	
		Date datClose = System.Today();
			
		// create & insert 1st
		Contact TestCon1 = UnitTestData.CreateTestContact ( 1 ) ;
		insert TestCon1;
		
		// now query to get the contact including Household Name updated by trigger
		id Con1Id = TestCon1.id;
		Contact Con1Upd = [SELECT id, ONEN_Household__c, Household_Name__c FROM Contact WHERE Id = :Con1Id LIMIT 1];
		
		//string HHName1 = Con1Upd.Household_Name__c;
		id HHId1 = Con1Upd.ONEN_Household__c;
		
		// create 2nd contact
		Contact TestCon2 = UnitTestData.CreateTestContact ( 2 ) ;
		// and set the HH name = to HHName from 1st Con
		// HH triggers should put them into a single HH
		TestCon2.ONEN_Household__c = HHId1;
		insert TestCon2;
		
//		system.debug(TestCon2.Household_Name__c);
		
		list<id> ConIDs = new list<id> ();
		ConIDs.add (Con1Id);
		ConIDs.add (TestCon2.id);
		
		// test to make sure HH was established properly
		Contact[] ContactsUpd = [SELECT id, ONEN_Household__c, Household_Name__c FROM Contact WHERE Id IN :ConIDs];
//		System.debug (ContactsUpd);
		System.AssertEquals ( ContactsUpd[0].ONEN_Household__c , ContactsUpd[1].ONEN_Household__c );
		
		// create a new membership opp(s) for the first contact
		// gotta put the one contact into an array first 'cause that's what the method expects
		Contact[] firstCon = new Contact[]{TestCon1};
		Opportunity[] newOpps = UnitTestData.OppsForContactList ( firstCon, null, 'Closed Won', datClose, 100 , 'Membership');

		//now set membership fields
		//TBD factor this out?
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// tbd set member level (after looking up picklist values??
			newOpps[i].Membership_Origin__c = 'New';
		}

		// insert the opp(s)
		Test.StartTest();
		insert newOpps;
		Test.StopTest();
		
		//now test that the both contacts in the HH received the proper member stats from the trigger
		Contact[] UpdatedCons = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id IN :ConIDs ];
		
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCons[0].Membership_Status__c );
		System.AssertEquals ( 1 , UpdatedCons[0].Membership_Span__c );
		System.AssertEquals ( 'New' , UpdatedCons[0].Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedCons[0].Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCons[0].Membership_End_Date__c );
		// tbd member level
		
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCons[1].Membership_Status__c );
		System.AssertEquals ( 1 , UpdatedCons[1].Membership_Span__c );
		System.AssertEquals ( 'New' , UpdatedCons[1].Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedCons[1].Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCons[1].Membership_End_Date__c );
	 	
	} 

	
	static testMethod void testProspectingMembership () {
	
		//for a contact, add a new prospecting membership opp
		//and test that member stats are not affected
		// then chg it to close won and test that stats are updated
	
		// insert a new contact and a new membership Opp (prospecting)
		integer howMany = 1;
		Date datClose = System.Today();
		
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
		Opportunity[] newOpps = UnitTestData.OppsForContactList ( TestCons, null, 'Prospecting', System.Today(), 100 , 'Membership');

		//now set membership fields
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// tbd set member level (after looking up picklist values??
			newOpps[i].Membership_Origin__c = 'New';
		}
		
		Test.StartTest();
		insert newOpps;
		Test.StopTest();
		
		// contact should still have no member status
		id FirstConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];		
		
		system.assertEquals ( null , UpdatedCon.Membership_End_Date__c);
		system.assertEquals ( null , UpdatedCon.Membership_Status__c);
		system.assertEquals ( null , UpdatedCon.Membership_Join_Date__c);
		
		// now chg something in the opp other than stage to closedwon
		newOpps[0].Amount = 33.33;
		update newOpps;
		
		// contact should still have no member status
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];		
		
		system.assertEquals ( null , UpdatedCon.Membership_End_Date__c);
		system.assertEquals ( null , UpdatedCon.Membership_Status__c);
		system.assertEquals ( null , UpdatedCon.Membership_Join_Date__c);
		
		// now finally chg to ClosedWon
		for ( Opportunity thisOpp : newOpps ) {
			thisOpp.StageName = 'Closed Won';
		}
		update newOpps;
		
		//tbd assert now has member status
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];				

		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( 1 , UpdatedCon.Membership_Span__c );
		System.AssertEquals ( 'New' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );
		
	} 


	static testMethod void testMultipleMemberships () {
	
		integer howMany = 3;
		date datClose = System.Today();
	
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		
		system.debug ('TEST>>>> adding Contacts...');
		insert TestCons;
		
		// create 4 opps for each contact, giving a 4-year membership history
		Opportunity[] newOpps = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datClose, 100 , 'Membership');
		Opportunity[] newOpps2 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', Date.newInstance (2006,10,14) , 100 , 'Membership');
		Opportunity[] newOpps3 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', Date.newInstance (2005,9,1) , 60 , 'Membership');
		Opportunity[] newOpps4 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', Date.newInstance (2004,11,7) , 30 , 'Membership');

		// also add one with no amount to test that
		Opportunity[] newOpps5 = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', Date.newInstance (2006,1,5) , null , 'Membership');

		// combine all the opps
		Opportunity[] allNewOpps = new list<Opportunity> ();
		allNewOpps.addAll (newOpps);
		allNewOpps.addAll (newOpps2);
		allNewOpps.addAll (newOpps3);
		allNewOpps.addAll (newOpps4);
		allNewOpps.addAll (newOpps5);

		// set membership fields in the opps
		for ( integer i=0;i<allNewOpps.size();i++) {
			allNewOpps[i].Membership_Start_Date__c = allNewOpps[i].CloseDate;
			allNewOpps[i].Membership_End_Date__c = allNewOpps[i].CloseDate.AddYears(1);
			// tbd set member level (after looking up picklist values??
			allNewOpps[i].Membership_Origin__c = 'New';
			allNewOpps[i].Member_Level__c = 'Bronze';
		}

//		system.debug ('TEST>>>> newOpps1 before insert :' + newOpps);

		system.debug ('TEST>>>> adding all Opps...');

		// and push em on up
		Test.StartTest();
		insert allNewOpps;
		Test.StopTest();	
		
		//	check that the contacts received the right membership stats		
		id FirstConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];				

		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( 4 , UpdatedCon.Membership_Span__c );  //is 4 right? or 5?
		System.AssertEquals ( 'New' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( Date.newInstance (2004,11,7) , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );

//		system.debug ('TEST>>>> newOpps2 before update: ' + newOpps2);
		system.debug ('TEST>>>> updating newOpps2...');

		// now change on the opps (both amount and date)
		// from the middle of the pack
		for ( Opportunity thisOpp : newOpps2 ) {
			thisOpp.Amount = 50;
			thisOpp.Membership_Start_Date__c = 	Date.newInstance (2006,2,14);
			thisOpp.Membership_End_Date__c = Date.newInstance (2007,2,14);
		}
		update newOpps2;
		
//		system.debug ('TEST>>>> newOpps2 after update: ' + newOpps2);
		
		//	check that the contacts membership stats haven't changed
		// (use the 2nd contact this time, just for variety)
		FirstConId = TestCons[1].id;
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];				

		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( 4 , UpdatedCon.Membership_Span__c );  //is 4 right? or 5?
		System.AssertEquals ( 'New' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( Date.newInstance (2004,11,7) , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );		

		system.debug ('TEST>>>> updating newOpps1 ...');
		
		// now change the amount/date of the final opp in the chain
		// this should affect member status
		// make it so membership ended 2 days ago (should be in grace period)
		for ( Opportunity thisOpp : newOpps ) {
			thisOpp.Amount = 25;
			thisOpp.Membership_Start_Date__c = 	datClose.addMonths (-2);
			thisOpp.Membership_End_Date__c = datClose.addDays (-2);
			thisOpp.Membership_Origin__c = 'Renewal';
		}
		update newOpps;

//		system.debug ('TEST>>>> newOpps1 after update: ' + newOpps);

		//	check that stats changed as expected
		FirstConId = TestCons[0].id;
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];				

		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_GRACE , UpdatedCon.Membership_Status__c );
		//System.AssertEquals ( 4 , UpdatedCon.Membership_Span__c );  //is 4 right? or 5?
		System.AssertEquals ( 'Renewal' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( Date.newInstance (2004,11,7) , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.addDays (-2) , UpdatedCon.Membership_End_Date__c );		
		
		
		// now chg the end date of one of the middle opps so that it's no the last
		// make sure the contact end date recalcs
		
		for ( Opportunity thisOpp : newOpps3 ) {
			thisOpp.Membership_Start_Date__c = datClose.addYears (2);
			thisOpp.Membership_End_Date__c = datClose.addYears (3);
		}
		update newOpps3;
		
		FirstConId = TestCons[2].id;
		UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];				
		System.AssertEquals ( datClose.addYears (3) , UpdatedCon.Membership_End_Date__c );		
		
	} 


	static testMethod void testNewMembershipAccount () {
	
		integer howMany = 1;
		Date datClose = System.Today();			
		
		// create & insert acct(s)
		Account[] TestAccts = UnitTestData.CreateMultipleTestAccounts ( howMany ) ;
		insert TestAccts;
		
		// and also a contact
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( 1 ) ;
		insert TestCons;		
		
		// create a new membership opp(s)
		Opportunity[] newOpps = UnitTestData.OppsForAccountList ( TestAccts, null, 'Closed Won', datClose, 100 , 'Membership');

		//now set membership fields, and put the contact as the primary ConRole
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// note for member level it's not required that the value actually be in the picklist
			// this won't work in the UI but works fine for tests
			newOpps[i].Member_Level__c = 'Phony Status';
			newOpps[i].Membership_Origin__c = 'New';
			newOpps[i].CR_Contact_Id__c = TestCons[0].id;
			newOpps[i].CR_Contact_Role__c = 'Decision Maker';
		}
		
		// insert the opp(s)
		Test.StartTest();
		insert newOpps;				
		Test.StopTest();

		//now test that the Account has received the proper member stats from the trigger
		id AcctId = TestAccts[0].id;
		Account UpdatedAcct = [SELECT Membership_Status__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Account WHERE Id = :AcctId];
		
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedAcct.Membership_Status__c );
		System.AssertEquals ( 'New' , UpdatedAcct.Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedAcct.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedAcct.Membership_End_Date__c );
		System.AssertEquals ( 'Phony Status' , UpdatedAcct.Member_Level__c );
		
		
		//also test that the Contact did not receive mbr stats
		id ConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :ConId];

		System.AssertEquals ( null , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( null , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( null , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( null , UpdatedCon.Membership_End_Date__c );
		System.AssertEquals ( null , UpdatedCon.Member_Level__c );
			
	}


	static testMethod void testProspectingMembershipAccount () {
	
		integer howMany = 1;
		Date datClose = System.Today();			
		
		// create & insert acct(s)
		Account[] TestAccts = UnitTestData.CreateMultipleTestAccounts ( howMany ) ;
		insert TestAccts;
		
		// and also a contact
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( 1 ) ;
		insert TestCons;		
		
		// create a new membership opp(s)
		Opportunity[] newOpps = UnitTestData.OppsForAccountList ( TestAccts, null, 'Prospecting', datClose, 100 , 'Membership');

		//now set membership fields, and put the contact as the primary ConRole
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// note for member level it's not required that the value actually be in the picklist
			// this won't work in the UI but works fine for tests
			newOpps[i].Member_Level__c = 'Phony Status';
			newOpps[i].Membership_Origin__c = 'New';
			newOpps[i].CR_Contact_Id__c = TestCons[0].id;
			newOpps[i].CR_Contact_Role__c = 'Decision Maker';
		}
		
		// insert the opp(s)
		Test.StartTest();
		insert newOpps;				
		Test.StopTest();

		//now test that the Account has received no member stats from the trigger yet
		id AcctId = TestAccts[0].id;
		Account UpdatedAcct = [SELECT Membership_Status__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Account WHERE Id = :AcctId];
		
		System.AssertEquals ( null , UpdatedAcct.Membership_Status__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_Origin__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_Join_Date__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_End_Date__c );
		System.AssertEquals ( null , UpdatedAcct.Member_Level__c );
				
		//also test that the Contact did not receive mbr stats
		id ConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :ConId];

		System.AssertEquals ( null , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( null , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( null , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( null , UpdatedCon.Membership_End_Date__c );
		System.AssertEquals ( null , UpdatedCon.Member_Level__c );
			
		// now update the opp w/ something other than ClosedWon (like the amount)
		newOpps[0].Amount = 33.33;
		update newOpps;
		
		//now test that the Account STILL has received no member stats from the trigger yet
		// this was failing at one point due to an incomplete SELECT statement in the class
		AcctId = TestAccts[0].id;
		UpdatedAcct = [SELECT Membership_Status__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Account WHERE Id = :AcctId];
		
		System.AssertEquals ( null , UpdatedAcct.Membership_Status__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_Origin__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_Join_Date__c );
		System.AssertEquals ( null , UpdatedAcct.Membership_End_Date__c );
		System.AssertEquals ( null , UpdatedAcct.Member_Level__c );
			
			
			
	}

	static testMethod void testNewMembershipsBulk () {
	
		// for a single contact w/ no previous mbrships, add a new membership
		// and test mbr stats are created
	
		integer howMany = 100;
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact[] TestCons = UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
//		system.debug (TestCons);
		
		// create a new membership opp(s)
		Opportunity[] newOpps = UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datClose, 100 , 'Membership');

		//now set membership fields
		for ( integer i=0;i<newOpps.size();i++) {
			newOpps[i].Membership_Start_Date__c = datClose;
			newOpps[i].Membership_End_Date__c = datClose.AddYears(1);
			// note for member level it's not required that the value actually be in the picklist
			// this won't work in the UI but works fine for tests
			newOpps[i].Member_Level__c = 'Phony Status';
			newOpps[i].Membership_Origin__c = 'New';
		}

		// insert the opp(s)
		Test.StartTest();
		insert newOpps;
		Test.StopTest();
		
		//now test that a contact has received the proper member stats from the trigger
		id FirstConId = TestCons[10].id;
		Contact UpdatedCon = [SELECT Membership_Status__c, Membership_Span__c, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Contact WHERE Id = :FirstConId];
		
		System.AssertEquals ( Constants.MEMBERSHIP_STATUS_CURRENT , UpdatedCon.Membership_Status__c );
		System.AssertEquals ( 1 , UpdatedCon.Membership_Span__c );
		System.AssertEquals ( 'New' , UpdatedCon.Membership_Origin__c );
		System.AssertEquals ( datClose , UpdatedCon.Membership_Join_Date__c );
		System.AssertEquals ( datClose.AddYears(1) , UpdatedCon.Membership_End_Date__c );
		System.AssertEquals ( 'Phony Status' , UpdatedCon.Member_Level__c );
		
		//also make sure that no member status has been given to the Individual account
		// new method to lookup individual acct id
		Id IndAcctId = DefaultAccount.GetIndividualAccountId();
		
		Account IndAcct = [SELECT id, Name, Membership_Origin__c, Membership_Join_Date__c, Membership_End_Date__c, Member_Level__c FROM Account WHERE Id = :IndAcctId LIMIT 1];

		System.AssertNotEquals ( null, IndAcct.Name );
		System.AssertEquals ( null , IndAcct.Membership_End_Date__c );
		System.AssertEquals ( null , IndAcct.Membership_Join_Date__c );
		System.AssertEquals ( null , IndAcct.Member_Level__c );		

	}

}